package com.woniushop.service.imple;

import java.util.List;

import com.woniushop.dao.AddressDAO;
import com.woniushop.dao.imple.AddressDAOImple;
import com.woniushop.entity.Address;
import com.woniushop.entity.PageBean;
import com.woniushop.service.AddressService;

public class AddressServiceImple implements AddressService {

	private AddressDAO addressDAO = new AddressDAOImple();

	/**
	 * 分页查找某用户的所有可用地址
	 * 
	 * @param pageBean
	 * @param uid
	 * @return
	 */
	@Override
	public List<Address> findValidByPage(PageBean pageBean, Address address) {
		// 完善pageBean信息
		Integer totalNum = addressDAO.count(address);
		pageBean.setTotalNum(totalNum);

		Integer pageSize = pageBean.getPageSize();
		Integer totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;
		totalPage = totalPage == 0 ? 1 : totalPage;
		pageBean.setTotalPage(totalPage);

		// 如果页码超过范围，则设为最后一页
		if (pageBean.getPage() > totalPage) {
			pageBean.setPage(totalPage);
		}

		// 查询地址数据
		List<Address> list = addressDAO.findByPage(pageBean.getPage(), pageSize, address);

		pageBean.setNumsOfCurrPage(list.size());

		return list;
	}

	/**
	 * 增加地址
	 * 
	 * @param address
	 */
	@Override
	public void add(Address address) {
		// 检查该用户是否有默认地址
		Address tempAddress = new Address();
		tempAddress.setUid(address.getUid());
		tempAddress.setAstate(1);
		// 如果没有默认地址，那么新地址就是默认地址
		if (addressDAO.count(tempAddress) == 0) {
			address.setAstate(1);
		} else {
			address.setAstate(2);
		}
		// 添加地址
		addressDAO.add(address);
	}

	/**
	 * 将uid对应的用户的aid地址改为默认地址
	 * 
	 * @param aid
	 * @param uid
	 */
	@Override
	public void setDefault(Integer aid, Integer uid) {
		Address defaultAddress = addressDAO.findDefault(uid);
		Address address = addressDAO.findOne(aid);
		defaultAddress.setAstate(2);
		address.setAstate(1);
		addressDAO.update(address);
		addressDAO.update(defaultAddress);
	}

	/**
	 * 删除地址
	 * 
	 * @param aid
	 */
	@Override
	public void del(Integer aid) {
		Address address = addressDAO.findOne(aid);
		// 如果要删除的地址是默认地址，则找一个非默认地址设为默认地址
		if (address.getAstate() == 1) {
			Address tempt = new Address();
			tempt.setUid(address.getUid());
			tempt.setAstate(2);
			List<Address> list = addressDAO.findByPage(1, 1, tempt);
			// 如果该地址已经是用户最后一个地址，则直接删除
			if (list != null && list.size() != 0) {
				tempt = list.get(0);
				tempt.setAstate(1);
				addressDAO.update(tempt);
			}
		}
		address.setAstate(3);
		addressDAO.update(address);
	}

	/**
	 * 根据id返回地址
	 * 
	 * @param aid
	 */
	@Override
	public Address findOne(Integer aid) {
		return addressDAO.findOne(aid);
	}

	@Override
	public void update(Address address) {
		// 找到原地址
		Address one = addressDAO.findOne(address.getAid());
		// 将原地址状态设置到新地址上；
		address.setAstate(one.getAstate());
		// 将原地址设为禁用
		one.setAstate(3);
		addressDAO.update(one);
		address.setAid(null);
		addressDAO.add(address);
	}

}
